<template>
	<van-list v-model="loading" :error.sync="error" error-text="请求失败，点击重新加载" :finished="finished" finished-text="没有更多了"
		@load="onLoad">
		<CommentItem v-for="(item, index) in list" :key="index" :comment="item" />
	</van-list>
</template>

<script>
	import {
		getComments
	} from '@/api/comment.js'
	import CommentItem from './comment-item.vue'

	export default {
		components: {
			CommentItem
		},
		
		props: {
			source: {
				type: [Number, String, Object],
				required: true
			},
			list: {
				type: Array,
				default: () => []
			}

		},

		data() {
			return {
				loading: false,
				finished: false,
				error: false,
				offset: null,
				limit: 10,
			}
		},

		created() {
			this.onLoad()
		},

		methods: {
			async onLoad() {
				try {
					const {
						data: res
					} = await getComments({
						type: 'a',
						source: this.source,
						offset: this.offset,
						limit: this.limit
					})
					const {
						results,
					} = res.data
					this.list.push(...results)

					this.$emit('onloadSuccess', res.data)

					this.loading = false
					if (results.length) {
						this.offset = res.data.last_id
					} else {
						this.finished = true
					}
				} catch (e) {
					this.loading = false
					this.error = true
				}
			},
		},
	}
</script>

<style scoped>

</style>
